import React from 'react'
import {Router} from 'dva/router'
import App from './routes/app'

// import {CookieUtils} from './utils'
// import Login from './routes/login'

export default function ({history, app}) {

  const checkLoginCookie = (nextState, replace) => {
  };
  const cached = {};

  function registerModel(app, model) {
    if (!cached[model.namespace]) {
      app.model(model);
      cached[model.namespace] = 1;
    }
  };
  const routes = [
    {
      path: '/',
      component: App,
      onEnter: checkLoginCookie,
      getIndexRoute(nextState, cb) {
        require.ensure([], require => {
          cb(null, {component: require('./routes/home')})
        });
      },
      childRoutes: [
        {
          path: 'configOne/:id',
          name: 'configOne',
          pathName: '配置流程',
          onEnter: checkLoginCookie,
          getIndexRoute(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/configOne/list'));
              cb(null, {component: require('./routes/configOne/list')})
            })
          }
        },
        {
          path: 'configColum/:id',
          name: 'configColum',
          pathName: '栏目流程',
          onEnter: checkLoginCookie,
          getIndexRoute(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/configColum/list'));
              cb(null, {component: require('./routes/configColum/list')})
            })
          }
        },
        {
          path: 'configColumAdd/:id/:name',
          name: 'configColumAdd',
          pathName: '栏目流程新增',
          onEnter: checkLoginCookie,
          getIndexRoute(nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/configColum/add'));
              cb(null, {component: require('./routes/configColum/add')})
            })
          }
        },
        {
          path: 'loading',
          name: 'loading',
          pathName: '配置流程',
          onEnter: checkLoginCookie,
          getIndexRoute(nextState, cb) {
            cb(null, {component: require('./routes/loading/list')});
          }
        },
        //routerStart//routerEnd
        {
          path: '*',
          name: 'error',
          onEnter: checkLoginCookie,
          getComponent(nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/home'))
            })
          }
        }
      ]
    }
  ];
  return <Router history={history} routes={routes}/>
}
